//
//  PPImgProcesser.cpp
//  Unity-iPhone
//
//  Created by unityask on 2019/3/21.
//

#include <stdio.h>
#include "PPImgProcesser.h"
#import "UIImage+OrientationFix.h"

@implementation PPImgProcesser

bool hasLoaded = false;
int frameCounter = 0;

+ (instancetype)sharedInstance {
    static PPImgProcesser *instance;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        instance = [[PPImgProcesser alloc] init];
    });
    return instance;
}

 - (void)setupWithImage:(UIImage*)image {
    UIImage* fixedImage = [image imageWithFixedOrientation];
     self.workingImage = fixedImage;
     self.mainImageView.image = fixedImage;
    
    // Commence with processing!
    [self logPixelsOfImage:fixedImage];
}

- (void)logPixelsOfImage:(UIImage*)image {
    // 1. Get pixels of image
    CGImageRef inputCGImage = [image CGImage];
    NSUInteger width = CGImageGetWidth(inputCGImage);
    NSUInteger height = CGImageGetHeight(inputCGImage);
    
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    
    //    UInt32 * pixels;
    self.pixels = (uint32_t *) calloc(height * width, sizeof(UInt32));
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(self.pixels, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), inputCGImage);
    
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    
#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
    
    // 2. Iterate and log!
    NSLog(@"Brightness of image:");
//    UInt32 * currentPixel = self.pixels;
//    for (NSUInteger j = 0; j < height; j++) {
//        for (NSUInteger i = 0; i < width; i++) {
//            UInt32 color = *currentPixel;
//            printf("%3.0f ", (R(color)+G(color)+B(color))/3.0);
//            currentPixel++;
//        }
//        printf("\n");
//    }
    
    //    free(pixels);
    
#undef R
#undef G
#undef B
    
}

- (void)show:(NSString *)title{
    if(hasLoaded){
        return;
    }
    hasLoaded = true;
   
//    [self setupWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"test%d.png",frameCounter]]];
    NSString* bundlePath = nil;
        bundlePath = [[NSBundle mainBundle] bundlePath];
        bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];

    NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
    UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"test" ofType:@"png"]];
    
    [self setupWithImage:image];
}

@end

extern "C" {
    void LoadImg(const char* title) {
        PPImgProcesser *picker = [PPImgProcesser sharedInstance];
        [picker show:nil];
    }
}

